/**
 * Created by dell on 2016/11/7.
 */
            $('.fileUpload').on('click',function(){
                $(this).next().trigger('click')
                return false;
            })
            //验证
            $(function(){var e = "<i class='fa fa-minus-circle'  style='font-size: 18px;padding-left: 5px;padding-top: 5px'></i> ";
                $('#typeForm').validate({
                    submitHandler:function(){
                        $('.btnNext').attr('href','locateAddBrand.html');
                        $.ajax({
                            type: "POST",
                            url: "RequestData.php",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(json),
                            dataType: "json",
                            success: function (message) {

                            },
                            error: function (message) {
                            }
                        });
                    },
                    rules:{
                        MainCategories:{
                            required:true,
                        },
                    },
                    messages:{
                        MainCategories:{
                            required:e+'请选择主营类目',
                        },
                    },
                    errorPlacement: function(error, element) { //错误信息位置设置方法
                        element.parent().append(error);
                    },
                })
                $('.btnNext').on('click',function(){
                    $('#TypeSubmit').trigger('click');
                })
            })

            $(function(){
                $.ajax({
                    url: "http://api.sku.tea.goetui.com/api/classify.ashx",
                    cache: false,
                    dataType: "jsonp",
                    type: "get",
                    data: { Action: "children", Data: JSON.stringify()},
                    timeout: 0,
                    success: function (result) {
                        if (result.IsSuccess) {
                            $('#catg_two').multipleSelect({
                                placeholder:'请选择',
                            })
                            var dataArray=result.Datas.List;
                            for (var i = 0; i < dataArray.length; i++) {
                                var provinceName = dataArray[i].class_nm;
                                var provinceID = dataArray[i].class_id;
                                $("#catg_one").append($("<option  value="+provinceID+','+provinceName+">" + provinceName + "</option>"));
                                $("#MainCategories").append($("<option value="+provinceName+">" + provinceName + "</option>"));
                            }
                            $("#catg_one").change(function () {
                                var provinceName = this.value.split(',');
                                document.getElementById("catg_two").innerHTML = " ";
                                for (var j = 0; j < dataArray.length; j++) {
                                    if (dataArray[j].class_nm == provinceName[1]) {
                                        var citys = dataArray[j].class_children;
                                        for(var k=0;k<citys.length;k++){
                                            var twoca=citys[k].class_nm;
                                            var twoid=citys[k].class_id;
                                            var cityOptions = $("<option  value="+twoid+','+twoca+">" + twoca + "</option>")
                                            $("#catg_two").append(cityOptions);
                                        }
                                    }
                                        $('#catg_two').multipleSelect('refresh');
                                }
                            })
                            $('.Brand').on('click',function(){
                                initTab(dataArray);
                            })
                        }else {
                            alert(result.Message);
                        }
                    }
                });
            });
            //删除事件
            window.operateEvents = {
                'click .remove': function (e, value, row) {
                    $table.bootstrapTable('remove', {
                        field: 'id',
                        values: [row.id]
                    });
                }
            };

            var json=[];
            var oneVal;
            var twoVal;
            function initTab(d){
                $(this).attr('disabled','true');
                for(var i=0;i< d.length;i++){
                    //console.log(d[i].class_nm);
                }
                setTimeout(function(){
                    $('.Brand').attr('disabled',false);
                },800)
                if(oneVal=='请选择'||twoVal=='请选择'){
                    return false;
                }else{
                    oneVal=document.getElementById('catg_one').value;
                    twoVal=$('#catg_two').val();
                    var thisTwo=twoVal[0].split(',')[1];
                    var thisTwoId=twoVal[0].split(',')[0];
                    var thisOne=oneVal.split(',')[1];
                    var thisOneId=oneVal.split(',')[0];
                    var row = {
                        ONE_ID:thisOneId,
                        ORDER_TYPE:thisOne,
                        ORDER_STATUS:thisTwo,
                        TWO_ID:thisTwoId,
                    };
                    var len=twoVal.length;
                    var twoArr=[];
                    var twoArrId=[];
                    if(len>1){
                        for(var k=0;k<len;k++){
                            thisTwo=twoVal[k].split(',')[1];
                            twoArr.push(thisTwo);
                            thisTwoId=twoVal[k].split(',')[0];
                            twoArrId.push(thisTwoId);
                        }
                        for(var i=0;i<len;i++){
                            var flag=true;
                            for(var j=0;j<json.length;j++) {
                                if (twoArr[i] == json[j].ORDER_STATUS) {
                                    flag=false;
                                }
                            }
                            if(flag){
                                row={
                                    ONE_ID:thisOneId,
                                    ORDER_TYPE:thisOne,
                                    ORDER_STATUS:twoArr[i],
                                    TWO_ID:twoArrId[i],
                                };
                                json.push(row);
                            }
                        }
                        $('#tableLM').bootstrapTable('load', json);
                        return;
                    }else{
                        var flag=true;
                        for(var j=0;j<json.length;j++) {
                            if (row.ORDER_STATUS == json[j].ORDER_STATUS) {
                                flag=false;
                            }
                        }
                        if(flag){
                            json.push(row);
                            $('#tableLM').bootstrapTable('load', json);
                        }
                    }
                }
            }
            //表格初始化
            var arr=[];
            var $table=$("#tableLM").bootstrapTable({
                pageSize: 1,
                cache: false,
                striped: true,
                pageList:[3],
                dataType: "json",
                locale: "zh-CN",
                uniqueId : "id",
                idField : "id",
                columns: [{
                    field : "id",
                    title : 'ID',
                    visible : false
                },{
                    title: '序号',
                    field: 'index',
                    formatter: function (value, row, index) {
                        return index+1;
                    }
                },{
                    title: 'ONE_ID',
                    field: 'ONE_ID',
                    visible : false
                }, {
                    field: 'ORDER_TYPE',
                    title: '一级目录'
                }, {
                    field: 'TWO_ID',
                    title: 'TWO_ID',
                    visible : false
                }, {
                    field: 'ORDER_STATUS',
                    title: '二级目录',
                    formatter:function(value, row, index){
                        for(var i=0;i<=index;i++){
                            arr.push(row.ORDER_STATUS);
                        }
                        return value;
                    }
                },{
                    field:'EDIT',
                    title:'操作',
                    events:operateEvents,
                    formatter: operateFormatter,
                },]
                }
            )
            //删除当前列
            $("#file-4").fileinput({
                uploadUrl: 'fileupload',
                language: 'zh',
                allowedFileExtensions : ['jpg', 'png','gif'],
                showUpload: false,
                showCaption: false,
                overwriteInitial: false,
                maxImageWidth: 400,
                showRemove:false,
                maxImageHeight: 200,
                maxFileSize: 200,
                dropZoneEnabled: false,
                maxFileCount: 2,
                showClose : false,
                msgFilesTooMany: "只能上传{m}张图片！",
                slugCallback: function(filename) {
                    return filename.replace('(', '_').replace(']', '_');
                },
            })
            function delInfo(id,index) {
                if(index+1==json.length){
                    json.pop();
                }else{
                    for(var i=index;i<json.length-1;i++){
                        json[i]=json[i+1];
                    }
                    json.pop();
                }
                if(!id){
                    $('#tableLM').bootstrapTable('load', json);
                    return;
                }
                $.ajax({
                    url : "/uteaAdmin/tasterGrade/del",
                    type : 'post',
                    contentType : 'application/json;charset=utf-8',
                    dataType : "json",
                    data : JSON.stringify({
                        id:id
                    }),
                    success : function(data) {
                        if (data.result) {
                            $('#fullTab').bootstrapTable('load', json);
                        }
                    }
                });
            }
            //初始化删除按钮
            function operateFormatter(value, row, index) {
                var json = jQuery.parseJSON(JSON.stringify(row));
                var id = json.id;
                return [
                    '<a class="remove btn btn-xs btn-default" onclick="delInfo('+id+','+index+')" style="margin-left:10px" title="删除">',
                    '<i class="fa fa-remove"></i>',
                    '</a>'
                ].join('')};